package com.milk.Dao;

import com.milk.DB.DBConnection;
import com.milk.entity.Members;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MembersDao {
    private static final Logger logger = Logger.getLogger(MembersDao.class.getName());

    // 查询所有会员信息
    public static List<Members> findAll() {
        List<Members> members = new ArrayList<>();
        String sql = "SELECT * FROM members";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql);
             ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                Members member = new Members();
                member.setId(resultSet.getInt("id"));
                member.setUserName(resultSet.getString("user_name"));
                member.setPhoneNumber(resultSet.getString("phone_number"));
                member.setPoints(resultSet.getInt("points"));
                members.add(member);
            }
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "查询所有会员信息失败", e);
        }
        return members;
    }

    // 根据id查询会员信息
    public static Members findById(int id) {
        Members member = null;
        String sql = "SELECT * FROM members WHERE id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, id);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                member = new Members();
                member.setId(resultSet.getInt("id"));
                member.setUserName(resultSet.getString("user_name"));
                member.setPhoneNumber(resultSet.getString("phone_number"));
                member.setPoints(resultSet.getInt("points"));
            }
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "根据ID查询会员信息失败", e);
        }
        return member;
    }

    // 添加会员
    public static int add(Members member) {
        String sql = "INSERT INTO members (user_name, phone_number, points) VALUES (?,?,?)";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, member.getUserName());
            statement.setString(2, member.getPhoneNumber());
            statement.setInt(3, member.getPoints());
            return statement.executeUpdate();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "添加会员失败", e);
            throw new RuntimeException("添加会员失败", e);
        }
    }

    // 根据id删除会员
    public static int deleteById(int id) {
        String sql = "DELETE FROM members WHERE id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, id);
            return statement.executeUpdate();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "删除会员失败", e);
            return -1;
        }
    }

    // 修改会员信息
    public static int update(Members member) {
        String sql = "UPDATE members SET user_name =?, phone_number =?, points =? WHERE id =?";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, member.getUserName());
            statement.setString(2, member.getPhoneNumber());
            statement.setInt(3, member.getPoints());
            statement.setInt(4, member.getId());
            return statement.executeUpdate();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "修改会员信息失败", e);
            throw new RuntimeException("修改会员信息失败", e);
        }
    }

    // 根据用户名模糊搜索会员信息
    public static List<Members> searchByUserName(String userName) {
        List<Members> members = new ArrayList<>();
        String sql = "SELECT * FROM members WHERE user_name LIKE CONCAT('%',?, '%')";
        try (Connection connection = DBConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, userName);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Members member = new Members();
                member.setId(resultSet.getInt("id"));
                member.setUserName(resultSet.getString("user_name"));
                member.setPhoneNumber(resultSet.getString("phone_number"));
                member.setPoints(resultSet.getInt("points"));
                members.add(member);
            }
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "根据用户名模糊搜索会员信息失败", e);
        }
        return members;
    }
}